package com.zj.jd_unlock

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import com.zj.jd_unlock.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() ,ILinkVIew {
    //保存9个点
    private lateinit var dotArray: Array<ImageView>
    private lateinit var binding: ActivityMainBinding
    //记录上一次被点亮的点的视图
    private var lastSelectedDot: ImageView? = null
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //创建绑定类的对象
        binding = ActivityMainBinding.inflate(layoutInflater)
        //将绑定类里面对应的view设置给activity
        setContentView(binding.root)
        val presenter = LinkPresenter(this)
        //保存所有的模型对象
        presenter.modelsArray = arrayListOf<LinkModel>()
        //将9个点的视图保存到数组中
        dotArray = arrayOf(
            binding.dot1,
            binding.dot2,
            binding.dot3,
            binding.dot4,
            binding.dot5,
            binding.dot6,
            binding.dot7,
            binding.dot8,
            binding.dot9
        )
        dotArray.forEach {
            presenter.modelsArray.add(LinkModel(it, R.drawable.dot_normal, R.drawable.dot_selected))
        }
        //竖线
        val verticalLineArray = arrayOf(
            binding.line14,
            binding.line25,
            binding.line36,
            binding.line47,
            binding.line58,
            binding.line69
        )
        verticalLineArray.forEach {
            presenter.modelsArray.add(
                LinkModel(
                    it,
                    R.drawable.line_1_normal,
                    R.drawable.line_1_error
                )
            )
        }
        //横线
        val landscapeLineArray = arrayOf(
            binding.line12,
            binding.line23,
            binding.line45,
            binding.line56,
            binding.line78,
            binding.line89
        )
        landscapeLineArray.forEach {
            presenter.modelsArray.add(
                LinkModel(
                    it,
                    R.drawable.line_2_normal,
                    R.drawable.line_2_error
                )
            )
        }
        //左斜
        val leftSlashLineArray =
            arrayOf(binding.line24, binding.line35, binding.line57, binding.line68)
        leftSlashLineArray.forEach {
            presenter.modelsArray.add(
                LinkModel(
                    it,
                    R.drawable.line_4_normal,
                    R.drawable.line_4_error
                )
            )
        }
        //右斜
        val rightSlashLineArray =
            arrayOf(binding.line15, binding.line26, binding.line48, binding.line59)
        rightSlashLineArray.forEach {
            presenter.modelsArray.add(
                LinkModel(
                    it,
                    R.drawable.line_3_normal,
                    R.drawable.line_3_error
                )
            )
        }

        //给容器添加触摸事件
        binding.container.setOnTouchListener { v, event ->

            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    presenter.down_Action(event)
                }

                MotionEvent.ACTION_MOVE -> {
                    val dotView = isInCircle(event.x,event.y)
                    if (lastSelectedDot != dotView){
                        if (dotView != null){
                            if (lastSelectedDot == null){
                                dotView.visibility = View.VISIBLE
                                lastSelectedDot = dotView
                            }
                        }
                    }
                }
                MotionEvent.ACTION_UP -> {
                    presenter.up_Action(binding)
                }
            }
            true
        }

    }

    override fun isInCircle(x: Float, y: Float): ImageView? {
        dotArray.forEach {
            if ((x >= it.left && x <= it.right) && (y >= it.top && y <= it.bottom)) {
                return it
            }
        }
        return null
    }


    override fun isFirstTouch() {
        TODO("Not yet implemented")
    }

    override fun changeImage(model: LinkModel, isHot: Boolean) {
        if (isHot) {
            model.view.setImageResource(model.normalResId)
        } else {
            model.view.setImageResource(model.errorResId)
        }
    }
}
//hjj6666666666666666666666